﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Drawing;
using System.Collections.ObjectModel;

//using WPFToolkit;
using TvdbConnector;
using TvdbConnector.Cache;
using TvdbConnector.Data;
using TvdbConnector.Data.Banner;
using TvdbConnector.Exceptions;
using System.Threading;

namespace DCShow_Browser
{
    public partial class ListeBanner
    {
        ObservableCollection<TvdbSeriesBanner> _BannerCollection =
        new ObservableCollection<TvdbSeriesBanner>();

        public ListeBanner()
        {
            this.InitializeComponent();
            Tvdb tvdbHandler = new Tvdb(new XmlCacheProvider(@"D:\\Temp\"), "69C6FDC7E5F4B985");
            tvdbHandler.InitCache();
            TvdbSeries s = tvdbHandler.GetFullSeries(73255, TvdbLanguage.DefaultLanguage, true);

            foreach (TvdbSeriesBanner b in s.SeriesBanners)
            {
                if (b.IsLoaded == false)
                {

                    //Thread toto;
                    

                    b.LoadBanner();

                }
                _BannerCollection.Add(b);
            }
            tvdbHandler.SaveCache();
            

        }
        public void GetBanner()
        {

        }


        public ObservableCollection<TvdbSeriesBanner> BannerCollection
        { get { return _BannerCollection; } }

    }

    /// <summary>
    /// Converter use to convert a System.Drawing.Image to an BitmapImage, when you need an ImageSource
    /// </summary>
    public class DrawingImageToImageSourceConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BitmapImage ImgSource = null;

            System.Drawing.Image image = value as System.Drawing.Image;

            if (image != null)
            {
                ImgSource = new BitmapImage();
                ImgSource.BeginInit();

                ImgSource.StreamSource = new MemoryStream(ConvertImageToByteArray(image));

                ImgSource.EndInit();
            }

            return ImgSource;
        }

        /// <summary>
        /// Convert a System.Drawing.Image to a byte array
        /// </summary>
        /// <param name="img">The System.Drawing.Image to convert in a byte array</param>
        /// <returns>A byte array</returns>
        private byte[] ConvertImageToByteArray(System.Drawing.Image img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

            return ms.ToArray();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }


}


